import pymysql

'''
数据连接参数
'''
host = "ring1"
port = 3306
db = "movie_analysis"
user = 'root'
password = 'root'

'''
数据库连接
'''
def get_connect():
    connect = pymysql.connect(host=host, port=port, db=db, user=user, password=password, use_unicode='utf-8')
    return connect

def sort_by_type():
    select = 'select id, type from movies'
    insert = 'insert into sortByType(movie_id, type) value (%s, %s)'

    connect = get_connect()

    cursor = connect.cursor()

    cursor.execute(select)

    movies = cursor.fetchall()

    for movie in movies:
        # print(movie)
        movie_id = movie[0]
        types = movie[1]

        for type in types.split('/'):
            # print(type)
            cursor.execute(insert, (movie_id, type))
            connect.commit()
